<template>
    <div class="login-container">
        <el-card class="login-card">
            <h2>系统登录</h2>
            <el-form :model="loginForm" :rules="rules" ref="loginForm">
                <el-form-item prop="username">
                    <el-input 
                        v-model="loginForm.username"
                        prefix-icon="el-icon-user"
                        placeholder="请输入用户名">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input 
                        v-model="loginForm.password"
                        prefix-icon="el-icon-lock"
                        type="password"
                        placeholder="请输入密码">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
    name: 'LoginView',
    data() {
        return {
            loginForm: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        handleLogin() {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    // 验证默认账户
                    if (this.loginForm.username === 'admin' && this.loginForm.password === '123456') {
                        this.$message.success('登录成功')
                        // 使用命名路由和params传递参数
                        this.$router.push({
                            name: 'main',
                            query: {
                                username: this.loginForm.username
                            }
                        })
                    } else {
                        this.$message.error('用户名或密码错误')
                    }
                } else {
                    return false
                }
            })
        }
    }
}
</script>

<style scoped>
.login-container {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f3f3f3;
}

.login-card {
    width: 400px;
}

.login-card h2 {
    text-align: center;
    margin-bottom: 20px;
    color: #409EFF;
}
</style>